window.Muni = window.Muni || {};
Muni.Marker = Muni.Marker || {};

//TODO description??
var defaultMarkerOpt = {
    latitude: 34.23,
    longitude: 23.23,
    clickable: false,
    draggable: false,    
    title: ""
};

var prepareMarkerOptions = function(cfg){
    options = {}
    $.extend(options, defaultMarkerOpt, cfg);
    return options;
}

Muni.Marker.google = (function(){
    var markers = [];

    return {
        addMarker: function(map, cfg){
            var options = prepareMarkerOptions(cfg);
            var latLng = new google.maps.LatLng(options.latitude, options.longitude);
            var icon = null;
            var markerOptions = {
                position: latLng,
                clickable: options.clickable,
                draggable: options.draggable,             
                title: options.title
            }
            var marker = new google.maps.Marker(markerOptions);
            markers.push(marker);
            marker.setMap(map);
            return marker;
        },

        getMarkers: function(){
            return markers;
        },

        removeMarker: function(marker){        
            var idx = markers.indexOf(marker);
            marker.setMap(null);
            if(idx != -1){
                markers.splice(idx, 1);
            }

        }
    }
})();

Muni.Marker.seznam = (function(){
    var layer;
    var markers = [];

    var prepareLayer = function(map){
        if(layer == null){
            layer = new SMap.Layer.Marker();
            map.addLayer(layer);
            layer.enable();
        }
    };    

    return {
        addMarker: function(map, cfg){
            prepareLayer(map);
            var options = prepareMarkerOptions(cfg);
            var coords = SMap.Coords.fromWGS84(options.longitude, options.latitude);
            var markerOptions = {
                active: options.clickable,
                title: options.title
            };
            var marker = new SMap.Marker(coords, markerOptions);
            if(options.draggable){
                marker.decorate(SMap.Marker.Feature.Draggable);
            }        
            layer.addMarker(marker);
            markers.push(marker);
            return marker;       
        },

        getMarkers: function(){
            return markers;
        },

        removeMarker: function(marker){
            layer.removeMarker(marker);
            var idx = markers.indexOf(marker);
            if(idx != -1){
                markers.splice(idx, 1);
            }
        }
    }

})();


Muni.Marker.bing = (function(){
    var markers = [];
    var map = null;
    
    return {
        addMarker: function(bmap, cfg){
            map = bmap;
            var options = prepareMarkerOptions(cfg);
            var position = new Microsoft.Maps.Location(options.latitude, options.longitude);
            var bOptions = {
                text: options.title,
                icon: options.icon,
                draggable: options.draggable
            };
            var shape = new Microsoft.Maps.Pushpin(position, bOptions);
            map.entities.push(shape);
            markers.push(shape);
            return shape;

        },

        getMarkers: function(){
            return markers;
        },

        removeMarker: function(map,marker){
            map.entities.remove(marker);
            var idx = markers.indexOf(marker);
            if(idx != -1){
                markers.splice(idx, 1);
            };            
        }
    };
})();

